package face.util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * Created by kriss on 2016/11/2.
 */
public class ImgUtils {

      public static JSONObject check(File file){
    	  JSONObject json=null;
    	  String content=null;
    	  //接口
    	  String url="https://api.megvii.com/facepp/v3/detect";
    	  HttpClient httpClient = new DefaultHttpClient();
          HttpPost httpPost  = new HttpPost(url);
          try {
			FileBody fb=new FileBody(file);
			HttpEntity he=MultipartEntityBuilder.create()
					.addPart("image_file", fb)
					.addTextBody("api_key", "Os124oyCT96Ty2AJg-VHanOLRg8IuyuS")
					.addTextBody("api_secret", "bkKlNac5G087Cl-3TTQkLy_aNZT5g-es")
					.addTextBody("return_landmark", "0")
					.addTextBody("return_attributes", "age,gender")
					.build();
			httpPost.setEntity(he);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
          
          try {
        	  HttpResponse response=httpClient.execute(httpPost);
  			content=EntityUtils.toString(response.getEntity());
  			System.out.println(content);
  			
  			JSONObject jb=JSONObject.parseObject(content);
  			JSONArray ja=jb.getJSONArray("faces");
  			String face_takon=ja.getJSONObject(0).getString("face_takon");
  			String age=ja.getJSONObject(0).getJSONObject("attributes").getJSONObject("age").getString("value");
  			String sex=ja.getJSONObject(0).getJSONObject("attributes").getJSONObject("gender").getString("value");
  			//获取特征码
  			String face=ja.getJSONObject(0).getString("face_token");
  			//取数据比对
  			
  			
  			//陌生人照片存放
  			SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmssSSS");
  			String imgPath="f:/imgs/"+sdf.format(new Date())+".png";
  			
  			json=JSON.parseObject("{\"age\":\""+age+"\""+", sex:\""+sex+"\"}");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    	  
      return json;
      }

    }